package com.uchedao.vehicledetection.view.carlist.m_service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.activity.CheckCarActivity;
import com.uchedao.vehicledetection.view.carlist.CarDbUtil;
import com.uchedao.vehicledetection.view.carlist.m_interface.MyInterface;

/**
 * Created by ZhangZhanghua on 2015/10/9 0009.
 */
public class updateDBService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        CarDbUtil.updateDb(updateDBService.this, mInterface);
    }

    private MyInterface mInterface = new MyInterface() {
        @Override
        public void onFinish(boolean b) {
            if (b == true) {
                Intent check = new Intent(updateDBService.this, CheckCarActivity.class);
                check.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                PendingIntent pendingIntent = PendingIntent.getActivity(updateDBService.this, 0, check, 0);
                NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notification = new Notification(R.mipmap.ic_launcher, "数据库更新完成",
                        System.currentTimeMillis());
                notification.setLatestEventInfo(getApplicationContext(), "车检单", "车型数据库更新完成", pendingIntent);
                notification.flags = Notification.FLAG_AUTO_CANCEL;
                manager.notify(1, notification);
                stopSelf();
            }
        }
    };
}
